package com.tools.common.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 * @author Admin
 */
public class DateUtils {

    /** 时间格式(yyyy-MM-dd) */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    public final static String DATE_YMDHS = "yyyyMMddHHmmss";
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * @title 获取yyyy-MM-dd HH:mm:ss格式的当前时间
     */
    public static String getNowDateTimeString() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }


    /**
     * 获取格式化时间
     *
     * @param fmt 格式化 DateUtils 的静态成员变量
     * @return 字符串时间
     */
    public static String getDateStr(String fmt) {
        return new SimpleDateFormat(fmt).format(new Date());
    }

    /**
     * @title 时间戳转yyyy-MM-dd HH:mm:ss格式的时间
     */
    public static String convertLong2DataTime(Long longTime) {
        if (longTime != null) {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(longTime));
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

    public static String longToDateStr(int longTime) {
        if (longTime != 0) {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(longTime * 1000L));
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }

    public static String msToDateStr(Long ms, String fmt) {
        if (ms != 0) {
            try {
                return new SimpleDateFormat(fmt).format(new Date(ms));
            } catch (Exception e) {
                return "";
            }
        }
        return "";
    }

    public static int htoMs(BigDecimal h) {
        if (null == h) {
            h = new BigDecimal(8.0);
        }
        float v = h.floatValue();
        return Math.round(v * 3600);
    }

    /**
     * 秒转小时
     *
     * @param seconds
     * @return
     */
    public static String secondsToTime(int seconds) {
        int h = seconds / 3600;            //小时
        int m = (seconds % 3600) / 60;        //分钟
        int s = (seconds % 3600) % 60;        //秒
        if (h > 0) {
            return h + "小时" + m + "分钟" + s + "秒";
        }
        if (m > 0) {
            return m + "分钟" + s + "秒";
        }
        return s + "秒";
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.longToDateStr(1646996432));
    }


    /**
     * 将秒数转换为日时分秒，
     *
     * @param second
     * @return
     */
    public static String secondToTime(long second) {
        long days = second / 86400;            //转换天数
        second = second % 86400;            //剩余秒数
        long hours = second / 3600;            //转换小时
        second = second % 3600;                //剩余秒数
        long minutes = second / 60;            //转换分钟
        second = second % 60;                //剩余秒数
        if (days > 0) {
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        } else {
            return hours + "小时" + minutes + "分" + second + "秒";
        }
    }

    /**
     * 将日期转换为日时分秒
     *
     * @param date
     * @return
     */
    public static String dateToTime(String date, String dateStyle) {
        SimpleDateFormat format = new SimpleDateFormat(dateStyle);
        try {
            Date oldDate = format.parse(date);
            long time = oldDate.getTime();                    //输入日期转换为毫秒数
            long nowTime = System.currentTimeMillis();        //当前时间毫秒数
            long second = nowTime - time;                    //二者相差多少毫秒
            second = second / 1000;                            //毫秒转换为妙
            long days = second / 86400;
            second = second % 86400;
            long hours = second / 3600;
            second = second % 3600;
            long minutes = second / 60;
            second = second % 60;
            if (days > 0) {
                return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
            } else {
                return hours + "小时" + minutes + "分" + second + "秒";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0) {
            return "00:00";
        } else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99) {
                    return "99:59:59";
                }
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + Integer.toString(i);
        } else {
            retStr = "" + i;
        }
        return retStr;
    }

    /**
     * 获取指定月份的时间 如获取3月前
     *
     * @param n n为正数 获取三月后 负数获取3月前
     * @return
     */
    public static String getMonthAoGo(int n) {
        Date dNow = new Date();   //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.MONTH, n);  //设置为前3月
        dBefore = calendar.getTime();   //得到前3月的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore);    //格式化前3月的时间
        String defaultEndDate = sdf.format(dNow); //格式化当前时间
        System.out.println("三个月之前时间=======" + defaultStartDate);
        System.out.println("当前时间===========" + defaultEndDate);
        return defaultStartDate;
    }

    /**
     * 获取指定月份的时间 月份
     *
     * @param n
     * @return
     */
    public static String getAoGOMonth(int n) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy_M");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();

        // 设置为当前时间
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, n);
        // 设置为上一个月
//        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + n);
        date = calendar.getTime();
        return format.format(date);
    }

    /**
     * 日期字符串转毫秒级时间戳
     *
     * @param dateStr
     * @return
     */
    public static Long dateStrToTimestamp(String dateStr, String fmt) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(fmt);
            Date dateStart = format.parse(dateStr);
            return (dateStart.getTime());
        } catch (Exception e) {
            throw new RuntimeException("日期字符串转换成时间戳出现异常:" + e.getMessage());
        }
    }

}
